/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Course;
import org.hibernate.Session;
import service.CourseDao;
import service.HibernateUtil;

/**
 *
 * @author kjeld
 */
public class DeleteCourseController extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session session = HU.openSession();
    private CourseDao dao = new CourseDao(session);

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println(request.getParameter("id"));
        if (request.getParameter("id") != null) {
            long id = Long.parseLong(request.getParameter("id"));
            //HttpSession session = request.getSession();
            Course course = dao.get(id);
            dao.delete(course);
            
            /*LinkedList<Course> courses = (LinkedList) session.getAttribute("courses");
            for (int i = 0; i < courses.size(); i++) {
                Course tempCourse = courses.get(i);

                if (tempCourse.getCourseID() == id) {
                    courses.remove(i);
                }
            }*/

            //session.setAttribute("courses", courses);
            response.sendRedirect("/course");
        }
    }
}
